<?php

/**
 * entidades actions.
 *
 * @package    auip
 * @subpackage entidades
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class entidadesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->entidads = Doctrine_Core::getTable('Entidad')->createQuery('a')->addOrderBy('a.nombre asc')->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->entidad = Doctrine_Core::getTable('Entidad')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->entidad);
  }

  public function executeNew(sfWebRequest $request)
  {
    //$this->form = new EntidadForm();
      $con = Doctrine_Manager::getInstance()->connection();
      $query = "SELECT id, nombre FROM tipo_entidad;";
      $st = $con->execute($query);
      $this->tipo_entidad = $st->fetchAll();
      
      $query1 = "SELECT id, nombre, activo FROM pais WHERE (activo = 1) ORDER BY(nombre);";
      $st1 = $con->execute($query1);
      $this->paises = $st1->fetchAll();
  }
/********************************************************************************************************************************************************/
  public function executeProcesador(sfWebRequest $request)
  {
      $con = Doctrine_Manager::getInstance()->connection();
      $id = $request->getParameter('id_pais');
      $id_region = $request->getParameter('id_region');
      $this->getUser()->setAttribute('id_region', $id_region);
      
      $query = "SELECT id, nombre FROM region WHERE (pais_id = ".$id.") AND (activo = 1) ORDER BY (nombre);";
      $st = $con->execute($query);
      echo '--->'.$id_region;
      $entidades = $st->fetchAll();
      $opciones = '';
                foreach ($entidades as $entidad) 
                {
                    $opciones.='<option value="'.$entidad[0].'">'.$entidad[1].'</option>';
                }      
     echo $opciones; 
  }    
  
  public function executeContinuar(sfWebRequest $request)
  {
      $valor1 = $request->getParameter('nombre');
      $valor2 = $request->getParameter('tipo');
      $valor3 = $request->getParameter('pais');
      $valor4 = $request->getParameter('region');
      $valor5 = $request->getParameter('debe');
      $this->Guardarentidad($valor1, $valor2, $valor3, $valor4, $valor5);            
      $this->redirect('entidades/index');
  }
  protected function Guardarentidad($nombre, $tipo_entidad, $id_pais, $id_region, $enmora)  
  {
         $con = Doctrine_Manager::getInstance()->connection();
         $query = "INSERT INTO entidad(nombre, pais_id, region_id, ciudad_id, en_mora, tipo_entidad) VALUES ('".$nombre."', '".$id_pais."', '".$id_region."',0,".$enmora.", ".$tipo_entidad.");";
         $st = $con->execute($query);
  }
  /*****************************************************************************************************************************************************/
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new EntidadForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($entidad = Doctrine_Core::getTable('Entidad')->find(array($request->getParameter('id'))), sprintf('Object entidad does not exist (%s).', $request->getParameter('id')));
    $this->form = new EntidadForm($entidad);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($entidad = Doctrine_Core::getTable('Entidad')->find(array($request->getParameter('id'))), sprintf('Object entidad does not exist (%s).', $request->getParameter('id')));
    $this->form = new EntidadForm($entidad);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($entidad = Doctrine_Core::getTable('Entidad')->find(array($request->getParameter('id'))), sprintf('Object entidad does not exist (%s).', $request->getParameter('id')));
    $entidad->delete();

    $this->redirect('entidades/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $entidad = $form->save();      
      $this->getUser()->setAttribute('msg', 'Se ha ADICIONADO un NUEVO item llamado-->'.$entidad->getNombre());
      $this->getUser()->setAttribute('id_entidad', $entidad->getId());            
      $this->redirect('becas/entidad');
    }
  }
}
